package cc.io24.knewcafe.boss.service;

import cc.io24.knewcafe.boss.dao.SysUserDao;
import cc.io24.knewcafe.boss.domain.SysUser;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginService {

    @Autowired
    private SysUserDao sysUserDao ;
    @Autowired
    private SysPermissionService sysPermissionService ;

    /**
     * 登录表单提交
     *
     * @param username,password
     * @return
     */
    public JSONObject authLogin(String username ,String password ) {
        JSONObject returnData = new JSONObject();
        Subject currentUser = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            currentUser.login(token);
            returnData.put("result", "success");
            returnData.put("username",username);
            returnData.put("roles",sysPermissionService.getPermissionsCodeStringArrayByUsername(username));

        } catch (AuthenticationException e) {
            returnData.put("result", "fail");
        }
        JSONObject resultJson = new JSONObject();
        resultJson.put("returnCode",  "100");
        resultJson.put("returnMsg", "请求成功");
        resultJson.put("returnData", returnData);

        return resultJson;
    }

    /**
     * 根据用户名和密码查询对应的用户
     *
     * @param username 用户名
     * @param password 密码
     * @return
     */
    public SysUser getUser(String username, String password) {
        return sysUserDao.findOneByUsernameAndPassword(username, password);
    }

    /**
     * 查询当前登录用户的权限等信息
     *
     * @return
     */
    public JSONObject getInfo() {
        //从session获取用户信息
        Session session = SecurityUtils.getSubject().getSession();
        JSONObject userInfo = (JSONObject) session.getAttribute("SESSION_USER_INFO");
        String username = userInfo.getString("username");
        JSONObject returnData = new JSONObject();
        JSONObject userPermission = null;//sysPermissionService.getUserPermission(username);
        session.setAttribute("SESSION_USER_PERMISSION", userPermission);
        returnData.put("userPermission", userPermission);
        JSONObject resultJson = new JSONObject();
        resultJson.put("returnCode",  "100");
        resultJson.put("returnMsg", "请求成功");
        resultJson.put("returnData", returnData);
        return resultJson;
    }

    /**
     * 退出登录
     *
     * @return
     */
    public JSONObject logout() {
        try {
            Subject currentUser = SecurityUtils.getSubject();
            currentUser.logout();
        } catch (Exception e) {
        }
        JSONObject resultJson = new JSONObject();
        resultJson.put("returnCode",  "100");
        resultJson.put("returnMsg", "请求成功");
        return resultJson;
    }


}
